﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;

namespace Com.CMS263.DAO
{
    public class TypeDAO
    {
        public TypeDAO() { }
        public bool Add(PO.Type type)
        {
            try
            {
                DBHelper.TransSaveOrUpdate(type);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public PO.Type GetTypeById(int id)
        {
            try
            {
                return DBHelper.ExecuteHQL("from Type where id=" + id).List<PO.Type>().First();
            }
            catch 
            {
                return null;
            }
        }
        public IList<PO.Type> GetAllType()
        {
            return DBHelper.ExecuteHQL("from Type").List<PO.Type>();
        }
        public bool ModifyType(PO.Type Type)
        {
            try
            {
                DBHelper.TransUpdate(Type);
                return true;
            }
            catch 
            {
                return false;
            }
        }
        public bool DeleteTypeById(int id)
        {
            try
            {
                DBHelper.ExecuteHQL("delete from Type as di where di.id=" + id).ExecuteUpdate();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public IList<PO.Type> GetTopType(int top)
        {
            return DBHelper.ExecuteHQL("from Type as ty order by ty.orderId desc").SetFirstResult(0).SetMaxResults(top).List<PO.Type>();

        }
        
    }
}
